package com.youdelu.downloadtest;

import androidx.appcompat.app.AppCompatActivity;

import android.Manifest;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.tbruyelle.rxpermissions2.RxPermissions;
import com.youdelu.downloadtest.downloadmanager.DownloadInfo;
import com.youdelu.downloadtest.downloadmanager.DownloadManager;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import io.reactivex.functions.Consumer;

public class MainActivity extends AppCompatActivity {

    private ProgressBar main_progress;
    private Button main_btn_down;
    private Button main_btn_pause;
    private Button main_btn_cancel;
    private DownloadInfo downloadInfo;
    private RxPermissions mRxPermission;

    /**
     * 文件下载地址
     */
    public final static String URL = "http://gdown.baidu.com/data/wisegame/4f032f45db533e52/fbc84f032f45db533e5240c586bccc4e.apk";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        EventBus.getDefault().register(this);
        //获取读写权限
        mRxPermission = new RxPermissions(this);
        mRxPermission.request(Manifest.permission.READ_EXTERNAL_STORAGE , Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .subscribe(aBoolean -> {
                    if (aBoolean){
                        init();
                    }else{
                        Toast.makeText(MainActivity.this , "请打开读写权限" , Toast.LENGTH_SHORT).show();
                    }
                });

    }

    private void init(){
        main_progress = findViewById(R.id.main_progress);
        main_btn_down = findViewById(R.id.main_btn_down);
        main_btn_pause = findViewById(R.id.main_btn_pause);
        main_btn_cancel = findViewById(R.id.main_btn_cancel);
        main_btn_down.setOnClickListener(view -> DownloadManager.getInstance().download(URL));
        main_btn_pause.setOnClickListener(view -> DownloadManager.getInstance().pauseDownload(URL));
        main_btn_cancel.setOnClickListener(view -> DownloadManager.getInstance().cancelDownload(downloadInfo));
    }

    //下载状态更新
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void update(DownloadInfo info){
        if (info.getUrl() != URL){
            return;
        }
        if (DownloadInfo.DOWNLOAD.equals(info.getDownloadStatus())){
            downloadInfo = info;
            if (info.getTotal() == 0){
                main_progress.setProgress(0);
            }else{
                float progress = info.getProgress() * main_progress.getMax() / info.getTotal();
                main_progress.setProgress((int) progress);
            }
        }else if (DownloadInfo.DOWNLOAD_OVER.equals(info.getDownloadStatus())){
            main_progress.setProgress(main_progress.getMax());
            Toast.makeText(this,"下载完成",Toast.LENGTH_SHORT).show();
        }else if (DownloadInfo.DOWNLOAD_PAUSE.equals(info.getDownloadStatus())){
            Toast.makeText(this,"下载暂停",Toast.LENGTH_SHORT).show();
        }else if (DownloadInfo.DOWNLOAD_CANCEL.equals(info.getDownloadStatus())){
            main_progress.setProgress(0);
            Toast.makeText(this,"下载取消",Toast.LENGTH_SHORT).show();
        }else if (DownloadInfo.DOWNLOAD_ERROR.equals(info.getDownloadStatus())){
            Toast.makeText(this,"下载出错",Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}